<template>

  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>会员功能</el-breadcrumb-item>
      <el-breadcrumb-item>兑换记录</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-form :inline="true" :model="queryinfo" class="demo-form-inline" size="mini">
        <el-form-item label="兑换单号">
          <el-input v-model="queryinfo.eid" placeholder="兑换单号" clearable/>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="queryinfo.mobile" placeholder="手机号" clearable/>
        </el-form-item>
        <el-form-item label="兑换状态">
          <el-select v-model="queryinfo.type" clearable filterable>
            <el-option label="提交申请" value="1"></el-option>
            <el-option label="发放成功" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间选择">
          <el-date-picker
              v-model="timer"
              type="datetimerange"
              range-separator="-"
              start-placeholder="Start date"
              end-placeholder="End date"
              :default-time="defaultTime"
              @change="changeData"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="GetData">查询</el-button>
        </el-form-item>
      </el-form>

      <hr>
      <el-table :data="tableData" fit stripe>
        <el-table-column prop="eid" label="单号"/>
        <el-table-column prop="account" label="兑换账号"/>
        <el-table-column prop="mobile" label="兑换手机号"/>
        <el-table-column prop="name" label="兑换产品" />
        <el-table-column prop="quota" label="兑换积分"/>
        <el-table-column prop="statu" label="兑换状态" width="180">
          <template #default="props">
            <el-switch v-if="props.row.type === 1" v-model="props.row.type"
                       active-color="#13ce66"
                       :active-value=2
                       active-text='放发'
                       :inactive-value=1
                       inactive-text='兑换申请'
                       @change="onChange(props.row)"
            />
            <el-tag type="success" v-else-if="props.row.type === 2" size="mini">发放成功</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="addtime" label="兑换时间" width="180">
          <template #default="props">{{ formatDate(props.row.addtime) }}</template>
        </el-table-column>
        <el-table-column prop="uptime" label="更新时间" width="180">
          <template #default="props">{{ formatDate(props.row.uptime) }}</template>
        </el-table-column>
      </el-table>
      <br>
      <!--分页-->
      <el-pagination
          background
          :current-page="queryinfo.page"
          :page-sizes="[3,10, 50, 100, 1000]"
          :page-size="queryinfo.size"
          layout="total, sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="changePage"
      />
    </el-card>
  </div>
</template>

<script>
import {onMounted, reactive, toRefs} from 'vue'
import axios from '@/utils/axios'
import {formatTimeToStr} from '@/utils/date'
import {ElMessage} from 'element-plus'

export default {
  name: 'Good',
  setup() {
    const state = reactive({
      queryinfo: {
        page: 1,
        size: 10
      },
      currentPage: 1, // 当前页
      pageSize: 10, // 分页大小
      total: 0,
      tableData: [],
      timer:[],
      defaultTime: [
        new Date(2000, 1, 1, 0, 0, 0),
        new Date(2000, 2, 1, 23, 59, 59),
      ],
    })
    onMounted(() => {
      GetData()

    })
    changeData
    const changeData = async (row) => {
      if (row !== null) {

        state.queryinfo.startTime = ((new Date(row[0])).getTime())/1000
        state.queryinfo.endTime = ((new Date(row[1])).getTime())/1000

      } else {
        delete state.queryinfo.startTime
        delete state.queryinfo.endTime
        console.log("取消时间")
      }
    }
    const onChange = async (row) => {
      console.log(row.id)
      const {data: res} = await axios.post('exchange', row)
      if (res.code !== 1000) return ElMessage.error(res.msg)
      ElMessage.success("成功")
      // state.drawer = false
      GetData()
    }
    // 获取列表
    const GetData = async () => {
      const queryinfo  = state.queryinfo
      const {data: res} = await axios.get('exchange', {params: queryinfo})
      if (res.code !== 1000) return ElMessage.error(res.msg)
      state.tableData = res.data.list
      state.total = res.data.total
    }
    const changePage = (val) => {
      console.log(11)
      state.queryinfo.page = val
      GetData()
    }

    const handleSizeChange = (val) => {
      state.queryinfo.size = val
      GetData()
    }

    const formatDate = (time) => {
      if (time !== null && time !== '') {
        if (time > 1000000000) {
          time = time * 1000
        }
        var date = new Date(time)
        return formatTimeToStr(date, 'yyyy-MM-dd hh:mm:ss')
      } else {
        return ''
      }
    }

    return {
      ...toRefs(state),
      GetData,
      changePage,
      formatDate,
      handleSizeChange,
      onChange,
      changeData
    }
  }
}

</script>

<style scoped>

</style>
